cmake_minimum_required(VERSION 3.21)
# Target definition
set(CMAKE_SYSTEM_NAME  Generic)
set(CMAKE_SYSTEM_PROCESSOR cortex-m3)

# Perform compiler test with static library
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

#---------------------------------------------------------------------------------------
# Set compilers
#---------------------------------------------------------------------------------------
set(CMAKE_C_COMPILER armclang CACHE INTERNAL "C Compiler")
set(CMAKE_CXX_COMPILER armclang CACHE INTERNAL "C++ Compiler")
set(CMAKE_ASM_COMPILER armasm CACHE INTERNAL "ASM Compiler")
set(CMAKE_LINKER armlink)
set(CMAKE_OBJCOPY fromelf)

# Set suffix of binary
set(CMAKE_EXECUTABLE_SUFFIX_C        .axf)
set(CMAKE_EXECUTABLE_SUFFIX_CXX      .axf)
set(CMAKE_C_OUTPUT_EXTENSION         .o)
set(CMAKE_CXX_OUTPUT_EXTENSION       .o)
set(CMAKE_STATIC_LIBRARY_SUFFIX_C    .a)
set(CMAKE_STATIC_LIBRARY_SUFFIX_CXX  .a)

# Project config
project(seekfree_mm32f3277_library C ASM)

set(CMAKE_C_FLAGS "-xc -std=c99 --target=arm-arm-none-eabi -mcpu=cortex-m3 -c -fno-rtti -fsigned-char -fshort-enums -D__MICROLIB -gdwarf-4 -O0 -ffunction-sections -Wno-packed -Wno-missing-variable-declarations -Wno-missing-prototypes -Wno-missing-noreturn -Wno-sign-conversion -Wno-nonportable-include-path -Wno-reserved-id-macro -Wno-unused-macros -Wno-documentation-unknown-command -Wno-documentation -Wno-license-management -Wno-parentheses-equality -MD")
set(CMAKE_ASM_FLAGS "--cpu Cortex-M3 -g --pd \"__MICROLIB SETA 1\"")

set(CMAKE_VERBOSE_MAKEFILE ON)

# Header directories
include_directories("SDK/CMSIS/Include")
include_directories("SDK/drivers")
include_directories("SDK/")
include_directories("zf_common/")
include_directories("zf_device/")
include_directories("zf_driver/")
include_directories("components/fatfs/")

# Source directories
set(SRC_DIR ${PROJECT_SOURCE_DIR}/)

aux_source_directory(${SRC_DIR}/SDK/drivers SDK_SRCS)
aux_source_directory(${SRC_DIR}/SDK SDK_SRCS)
aux_source_directory(${SRC_DIR}/zf_common DRIVER_SRCS)
aux_source_directory(${SRC_DIR}/zf_driver DRIVER_SRCS)
aux_source_directory(${SRC_DIR}/zf_device DEVICE_SRCS)
aux_source_directory(${SRC_DIR}/components/fatfs/source DEVICE_SRCS)

add_library(seekfree STATIC "sdk/mdk/startup_mm32f327x_keil.s" ${SDK_SRCS} ${DRIVER_SRCS} ${DEVICE_SRCS})

file(REMOVE_RECURSE "../include" "../lib")
file(MAKE_DIRECTORY "../lib")

file(COPY "SDK/CMSIS/Include/" DESTINATION ../../include/cmsis FILES_MATCHING PATTERN "*.h")
file(COPY "SDK/drivers/" DESTINATION ../../include/drivers FILES_MATCHING PATTERN "*.h")

file(GLOB files LIST_DIRECTORIES false sdk/*.h)
file(COPY ${files} DESTINATION ../../include/mm32f3277)

file(COPY "zf_common/" DESTINATION ../../include/zf_common FILES_MATCHING PATTERN "*.h")
file(COPY "zf_driver/" DESTINATION ../../include/zf_driver FILES_MATCHING PATTERN "*.h")
file(COPY "zf_device/" DESTINATION ../../include/zf_device FILES_MATCHING PATTERN "*.h")
file(COPY "components/fatfs/source/" DESTINATION ../../include/fatfs FILES_MATCHING PATTERN "*.h")

add_custom_command(TARGET seekfree
    POST_BUILD
    COMMAND ${CMAKE_AR} -x ../zf_device/zf_device_config.a 
    COMMAND ${CMAKE_AR} -r libseekfree.a zf_device_config.o 
    COMMAND ${CMAKE_COMMAND} -E copy libseekfree.a ../../lib
)

